package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppRecommentGroupDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppRecommentDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppRecommentGroupDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppRecommentGroupEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppRecommentGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppRecommentService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送组相关接口实现类
 * @author chendi
 * @date 2018年2月1日
 */
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class MallAppRecommentGroupServiceImpl implements MallAppRecommentGroupService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppRecommentGroupServiceImpl.class);
    
    private MallAppRecommentGroupDao mallAppRecommentGroupDao;
    private MallAppRecommentService mallAppRecommentService;

    @Autowired
    public MallAppRecommentGroupServiceImpl(MallAppRecommentGroupDao mallAppRecommentGroupDao, MallAppRecommentService mallAppRecommentService) {
        this.mallAppRecommentGroupDao = mallAppRecommentGroupDao;
        this.mallAppRecommentService = mallAppRecommentService;
    }

    @Override
    public List<MallAppRecommentGroupDTO> queryList(MallAppRecommentGroupDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT gro.* FROM mall_app_recomment_group gro WHERE gro.is_delete = 0 AND gro.arg_name like '%'||:argName||'%' AND gro.arg_status =:argStatus  ORDER BY arg_status,arg_predict_start_time ";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return mallAppRecommentGroupDao.selectListDTOBySql(param);
    }

    @Override
    public MallAppRecommentGroupDTO load(MallAppRecommentGroupDTO dto, Object id) throws Exception {
        MallAppRecommentGroupEntity recommentEntity = mallAppRecommentGroupDao.queryBy("arg_id",id);
        if(null == recommentEntity){
            return null;
        }
        MallAppRecommentGroupDTO recommentDTO = new MallAppRecommentGroupDTO();
        ReflectUtils.copyPropertiesIgnoreNull(recommentEntity,recommentDTO);
        return recommentDTO;
    }

    @Override
    public MallAppRecommentGroupDTO saveOrUpdate(MallAppRecommentGroupDTO dto) throws Exception {
        MallAppRecommentGroupEntity recommentGroupEntity = null;
        if (null != dto.getArgId()) {
            recommentGroupEntity = mallAppRecommentGroupDao.queryBy("arg_id", dto.getArgId());
            if (null == recommentGroupEntity) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Recommentid为" + dto.getArgId() + "的推送信息，保存失败");
            }
        } else {
            recommentGroupEntity = new MallAppRecommentGroupEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, recommentGroupEntity);
        if (null != dto.getArgId()) {
            mallAppRecommentGroupDao.baseUpdateByPrimaryKey(recommentGroupEntity);
        } else {
            mallAppRecommentGroupDao.baseInsert(recommentGroupEntity);
        }
        if(null == recommentGroupEntity.getArgId()){
            throw new MsgException(ResponseCodeEnum.NO_DATA,"系统异常，保存失败");
        }
        List<Integer> recommentItem = new ArrayList<>();
        if(null != dto.getMallAppRecomments() && dto.getMallAppRecomments().size() > 0){
            for(MallAppRecommentDTO recomment:dto.getMallAppRecomments()){
                recomment.setArgId(recommentGroupEntity.getArgId());
                recomment.setUid(dto.getUid());
                recomment.setUname(dto.getUname());
                mallAppRecommentService.saveOrUpdate(recomment);
                recommentItem.add(recomment.getAbtId());
            }
        }
        MallAppRecommentDTO recommentDTO = new MallAppRecommentDTO();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,recommentDTO);
        mallAppRecommentService.deleteRecommentByIds(recommentDTO,recommentItem,recommentGroupEntity.getArgId());
        ReflectUtils.copyPropertiesIgnoreNull(recommentGroupEntity, dto);
        return dto;
    }

    @Override
    public int delete(MallAppRecommentGroupDTO dto, Object id, boolean realDel) throws Exception {
        MallAppRecommentGroupEntity recommentEntity = mallAppRecommentGroupDao.queryBy("arg_id", id);
        if (null == recommentEntity) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Recommentid为" + id + "的推送信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,recommentEntity);
            recommentEntity.setIsDelete(1);
            return mallAppRecommentGroupDao.baseUpdateByPrimaryKey(recommentEntity);
        }
        return mallAppRecommentGroupDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<MallAppRecommentGroupDTO> queryAppRecommentGroupAndInitChild(MallAppRecommentGroupDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT gro.* FROM mall_app_recomment_group gro WHERE gro.is_delete = 0 AND gro.arg_name like '%'||:argName||'%' AND gro.arg_status =:argStatus  ORDER BY arg_status,arg_predict_start_time ";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        List<MallAppRecommentGroupDTO> mallAppRecommentGroupList = mallAppRecommentGroupDao.selectListDTOBySql(param);
        if(null != mallAppRecommentGroupList && mallAppRecommentGroupList.size() > 0){
            MallAppRecommentDTO appRecomment = new MallAppRecommentDTO();
            for(MallAppRecommentGroupDTO mallAppRecommentGroup:mallAppRecommentGroupList){
                appRecomment.setArgId(mallAppRecommentGroup.getArgId());
                mallAppRecommentGroup.setMallAppRecomments(mallAppRecommentService.queryList(appRecomment,false));
            }
        }
        return mallAppRecommentGroupList;
    }
}
